package org.bt.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.bt.common.constant.HttpConstant;

import java.util.Map;

/**
 * map与url参数转换
 *
 * @author tongw
 * @date 2021/10/22
 */
public class MapUrlParamsUtil {

    /**
     * 将map转换成url
     *
     * @param map map
     *
     * @return url
     */
    public static String getUrlParamsByMap(Map<String, Object> map) {
        if (map == null) {
            return "";
        }
        StringBuilder buffer = new StringBuilder();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            buffer.append(entry.getKey()).append(HttpConstant.Sign.EQUAL).append(entry.getValue());
            buffer.append(HttpConstant.Sign.AND);
        }
        String s = buffer.toString();
        if (s.endsWith(HttpConstant.Sign.AND)) {
            s = StringUtils.substringBeforeLast(s, HttpConstant.Sign.AND);
        }
        return s;
    }
}
